﻿//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Resource Application Block
//===============================================================================

using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder;
using Microsoft.Practices.ObjectBuilder2;
using EntLibContrib.Resource.Instrumentation;

namespace EntLibContrib.Resource.Configuration
{
	/// <summary>
	/// This type supports the Enterprise Library infrastructure and is not intended to be used directly from your code.
	/// Represents the process to build a <see cref="ResourceManager"/> described by a <see cref="ResourceManagerData"/> configuration object.
	/// </summary>
	/// <remarks>This type is linked to the <see cref="ResourceManagerDataBase"/> type and it is used by the <see cref="ResourceManagerCustomFactory"/> 
	/// to build the specific <see cref="IResourceManager"/> object represented by the configuration object.
	/// </remarks>	
	public class ResourceManagerAssembler : IAssembler<IResourceManager, ResourceManagerDataBase>
	{
		#region Public methods
		/// <summary>
		/// This method supports the Enterprise Library infrastructure and is not intended to be used directly from your code.
		/// Builds a <see cref="ResourceManager"/> based on an instance of <see cref="ResourceManagerData"/>.
		/// </summary>
		/// <param name="context">The <see cref="IBuilderContext"/> that represents the current building process.</param>
		/// <param name="objectConfiguration">The configuration object that describes the object to build. Must be an instance of <see cref="ResourceManagerData"/>.</param>
		/// <param name="configurationSource">The source for configuration objects.</param>
		/// <param name="reflectionCache">The cache to use retrieving reflection information.</param>
		/// <returns>
		/// A fully initialized instance of <see cref="ResourceManager"/>.
		/// </returns>
		/// <seealso cref="ResourceManagerCustomFactory"/>
		public IResourceManager Assemble(IBuilderContext context,
																		ResourceManagerDataBase objectConfiguration,
																		IConfigurationSource configurationSource,
																		ConfigurationReflectionCache reflectionCache)
		{
			// cast the configuration data to the specifc data type
			ResourceManagerData castObjectConfiguration = (ResourceManagerData)objectConfiguration;
			// get the custom command associated with this query
			IResourceProvider resourceProvider = ResourceManagerAssemblerHelper.CreateResourceProvider(castObjectConfiguration.ResourceProvider, configurationSource);
			// add an instrument provider
			ResourceInstrumentationProvider instrumentationProvider = ResourceManagerAssemblerHelper.CreateInstrumentationProvider(castObjectConfiguration.Name, configurationSource, reflectionCache);
			// create the resource manager
			ResourceManager createdObject = ResourceManagerAssemblerHelper.BuildResourceManager(castObjectConfiguration.Name,
																																													resourceProvider,
																																													instrumentationProvider);
			// return the fully built and populated resource manager object
			return createdObject;
		}
		#endregion
	}
}
